<?php
class DbHelper//并没有做安全性健壮性的处理
{
	private $db;//数据库连接对象PDO
	private $field;//检索的字段
	private $where;//检索条件
	private $table;//检索表
	private $prefix;//表前缀
	private $order;//排序

	public function __construct($table)//构造
	{
		try{
			require 'Config.php';
			$this->db=new PDO($config['dsn'],$config['user'],$config['password']);
			$this->prefix=$config['prefix'];
			unset($config);
			$this->table=$this->prefix.$table;
		}catch(PDOException $e){
			echo 'Connection failed: ' . $e->getMessage();
		}
	}

	public function table($table)//指定查询表
	{
		$this->table=$prefix.$table;
		return $this;
	}

	public function where($where)//指定查询条件
	{
		$this->where=$where;
		return $this;
	}

	public function field($field)//指定查询字段
	{
		$this->field=$field;
		return $this;
	}

	public function order($order)//指定排序
	{
		$this->order=$order;
		return $this;
	}

	/* @return array 查询结果
	 */
	public function select()//执行查询
	{
		$sql='SELECT ';
		$sql.=empty($this->field)?'*':$this->field;
		$sql.=' FROM '.$this->table;
		$sql.=empty($this->where)?'':' WHERE '.$this->where;
		$sql.=empty($this->order)?'':' ORDER BY '.$this->order;
		$statement=$this->db->query($sql);
		$statement->setFetchMode(PDO::FETCH_ASSOC);
		return $statement->fetchAll();
	}

	/* @return int 影响数量
	 */
	public function delete()//执行删除
	{
		$sql='DELETE ';
		$sql.=' FROM '.$this->table;
		$sql.=empty($this->where)?'':' WHERE '.$this->where;
		
		return $this->db->exec($sql);
	}

}